const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
// 页面下面票据明细
router.post('/bill/getInvoiceDetail', async ctx => {
    let user = ctx.user
    let body = ctx.request.body
    let tenantId = user.tenantId;
    let transType = tool.xe.toNumber(body.transType)
    let billNo = body.billNo ? body.billNo : '';

    if(!billNo) return
    if(transType && transType === 190001) {
        let list = await db.models['cardInfo'].findAll({
            where: {
                tenantId,
                billNo: billNo
            }
        })
        return ctx.suc('', list)
    }
    else {
        let bill = await db.models['invoice'].findOne({
            where: {
                tenantId,
                billNo
            }
        })
        let billType = bill.billType
        if (['SALE', 'PUR', 'OO', 'OI'].indexOf(billType) !== -1) {

            let list = await db.models['invoiceInfo'].findAll({
                where: {
                    tenantId,
                    iid: bill.id
                }
            })
            return ctx.suc('', list)
        }
        if (billType==='RECEIPT' || billType==='PAYMENT') {
            let list = await db.models['invoiceReceipt'].findAll({
                where: {
                    tenantId,
                    iid: bill.id
                }
            })
            return ctx.suc('', list)
        }
        if (['ZZ', 'YSK','YFK','ZC','SR','YS','YF']) {
            let list = await db.models['invoiceOther'].findAll({
                where: {
                    tenantId,
                    iid: bill.id
                }
            })
            return ctx.suc('', list)
        }

        if (billType==='YSG') {
            let list = await db.models['invoiceInfoYs'].findAll({
                where: {
                    tenantId,
                    iid: bill.id
                }
            })
            return ctx.suc('', list)
        }
    }
    return ctx.suc('', [])
})

module.exports = router;